let ProdTools = {};
//let prods = {}; //未来从localStorage中获取 没有数据就因该是空对象

let store = window.localStorage;

let prods = JSON.parse(store.getItem('prods') || '{}');
//{id:sum}

//增加或者追加 
ProdTools.addOrUpDate = function (p) {
    if (prods[p.id]) {
        prods[p.id] += p.num;
    } else {
        prods[p.id] = p.num;
    }
    //存储到本地
    this.saveProds(prods);

}

//删除
ProdTools.delete = function (id) {
    delete prods[id];

    //删除之后存储到本地
    this.saveProds(prods);


}

//获取
ProdTools.getProds = function () {
    // return prods;
    return JSON.parse(store.getItem('prods') || '{}');

}

//获取总数
ProdTools.getTotalCount = function(){
    let sum = 0;
    for(let id in prods){
        sum += prods[id];
    }
    return sum;
}

//存储
ProdTools.saveProds = function(prods){
    store.setItem('prods',JSON.stringify(prods));
}
export default ProdTools;